import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../widgets/platform/adaptive_card.dart';
import '../../widgets/platform/adaptive_button.dart';
import '../../widgets/platform/responsive_layout.dart';
import '../../../core/platform/platform_detector.dart';

/// 需求列表页面
class RequirementListPage extends ConsumerWidget {
  const RequirementListPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      appBar: AppBar(
        title: const SelectableText('需求管理'),
        actions: [
          if (PlatformDetector.isDesktop() || PlatformDetector.isWeb())
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: AdaptiveButton(
                onPressed: () {
                  // TODO: 实现创建需求功能
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: SelectableText('创建需求功能开发中...')),
                  );
                },
                child: const SelectableText('创建需求'),
              ),
            ),
        ],
      ),
      body: LayoutBuilder(
        builder: (context, constraints) {
          return _buildContent(context, constraints);
        },
      ),
      floatingActionButton:
          !(PlatformDetector.isDesktop() || PlatformDetector.isWeb())
              ? FloatingActionButton(
                  onPressed: () {
                    // TODO: 实现创建需求功能
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: SelectableText('创建需求功能开发中...')),
                    );
                  },
                  child: const Icon(Icons.add),
                )
              : null,
    );
  }

  Widget _buildContent(BuildContext context, BoxConstraints constraints) {
    // 模拟需求数据
    final requirements = [
      {
        'id': 1,
        'title': '用户登录功能',
        'description': '实现用户注册、登录和权限验证功能',
        'priority': '高',
        'status': '进行中',
        'project': '电商平台优化项目',
        'assignee': '张三',
      },
      {
        'id': 2,
        'title': '购物车功能',
        'description': '实现商品添加、删除、修改数量等购物车功能',
        'priority': '中',
        'status': '待办',
        'project': '电商平台优化项目',
        'assignee': '李四',
      },
      {
        'id': 3,
        'title': '支付功能集成',
        'description': '集成第三方支付平台，支持多种支付方式',
        'priority': '高',
        'status': '阻塞',
        'project': '电商平台优化项目',
        'assignee': '王五',
      },
      {
        'id': 4,
        'title': '订单管理',
        'description': '实现订单查询、状态跟踪等功能',
        'priority': '中',
        'status': '已完成',
        'project': '电商平台优化项目',
        'assignee': '赵六',
      },
    ];

    return RefreshIndicator(
      onRefresh: () async {
        // TODO: 实现刷新功能
      },
      child: ResponsiveGrid(
        spacing: 16,
        runSpacing: 16,
        children: [
          // 统计卡片
          _buildStatsCard(context),

          // 需求列表
          for (final requirement in requirements)
            _buildRequirementCard(context, requirement),
        ],
      ),
    );
  }

  Widget _buildStatsCard(BuildContext context) {
    return AdaptiveCard(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SelectableText(
            '需求统计',
            style: Theme.of(context).textTheme.titleLarge,
          ),
          const SizedBox(height: 16),
          Row(
            children: [
              _buildStatItem(context, '总计', '12', Colors.blue),
              _buildStatItem(context, '进行中', '5', Colors.orange),
              _buildStatItem(context, '已完成', '7', Colors.green),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStatItem(
      BuildContext context, String label, String value, Color color) {
    return Expanded(
      child: Column(
        children: [
          SelectableText(
            value,
            style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                  color: color,
                  fontWeight: FontWeight.bold,
                ),
          ),
          SelectableText(
            label,
            style: Theme.of(context).textTheme.bodyMedium,
          ),
        ],
      ),
    );
  }

  Widget _buildRequirementCard(
      BuildContext context, Map<String, dynamic> requirement) {
    final priority = requirement['priority'] as String;
    final status = requirement['status'] as String;

    Color priorityColor;
    switch (priority) {
      case '高':
        priorityColor = Colors.red;
        break;
      case '中':
        priorityColor = Colors.orange;
        break;
      case '低':
        priorityColor = Colors.green;
        break;
      default:
        priorityColor = Colors.grey;
    }

    Color statusColor;
    switch (status) {
      case '待办':
        statusColor = Colors.grey;
        break;
      case '进行中':
        statusColor = Colors.blue;
        break;
      case '已完成':
        statusColor = Colors.green;
        break;
      case '阻塞':
        statusColor = Colors.red;
        break;
      default:
        statusColor = Colors.grey;
    }

    return AdaptiveCard(
      onTap: () {
        // TODO: 实现查看详情功能
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('查看详情功能开发中...')),
        );
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(
                child: Text(
                  requirement['title'] as String,
                  style: Theme.of(context).textTheme.titleMedium?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: priorityColor.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(12),
                  border:
                      Border.all(color: priorityColor.withValues(alpha: 0.3)),
                ),
                child: Text(
                  priority,
                  style: TextStyle(
                    color: priorityColor,
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            requirement['description'] as String,
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  color: Colors.grey[600],
                ),
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
          const SizedBox(height: 12),
          Row(
            children: [
              _buildInfoChip(
                  Icons.folder, requirement['project'] as String, Colors.blue),
              const SizedBox(width: 8),
              _buildInfoChip(Icons.person, requirement['assignee'] as String,
                  Colors.green),
              const Spacer(),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: statusColor.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  status,
                  style: TextStyle(
                    color: statusColor,
                    fontSize: 12,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildInfoChip(IconData icon, String text, Color color) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            icon,
            size: 14,
            color: color,
          ),
          const SizedBox(width: 4),
          Text(
            text,
            style: TextStyle(
              color: color,
              fontSize: 12,
            ),
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }
}
